<template>
  <div class="login-container">
    <div class="login_box">
      <!-- 登录logostart -->
      <div class="login_logo">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <!-- 登录logoend -->

      <!-- 登录表单start -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button @click="login" type="primary">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 登录表单end -->
    </div>
  </div>
</template>

<script>
/* api调用 */
import { postLoginApi } from '@/api/LoginApi/LoginApi.js'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
    }
  },
  methods: {
    /* 表单部分start */
    //表单重置
    resetForm() {
      this.$refs.loginFormRef.resetFields()
    },
    //表单登录
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await postLoginApi(this.loginForm)
          if (res.meta.status !== 200) {
            this.$message.error('登录失败')
          } else {
            this.$message.success('登录成功')
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          }
        } else {
          return
        }
      })
    },
    /* 表单部分end */
  },
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_logo {
  width: 130px;
  height: 130px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    background-color: #fff;
    border-radius: 50%;
    padding: 10px;
    box-sizing: border-box;
    border: 10px solid #eee;
    box-shadow: 0 -5px 10px #eee;
  }
}
.el-form {
  width: 100%;
  padding: 0 40px;
  margin-top: 20px;
  box-sizing: border-box;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .el-form-item {
    width: 240px;
    margin: 20px auto;
  }
}
.login_btn {
  display: flex;
  justify-content: space-around;
}
</style>
